package com.yz.test.parking.getway;


import com.yz.test.parking.http.HttpClientManager;
import com.yz.test.parking.http.HttpRequestResult;
import com.yz.test.parking.sign.SignatureHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/yz/parking")
public class PostYZOpenGetway {

    @RequestMapping(value="/sendTradePay",method= RequestMethod.POST)
    public ModelAndView sendTradePay(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("app_auth_token",req.getParameter("app_auth_token"));
        bizContentparams.put("pay_type",req.getParameter("pay_type"));
        bizContentparams.put("mch_id",req.getParameter("mch_id"));
        bizContentparams.put("sub_mchid",req.getParameter("sub_mchid"));
        bizContentparams.put("official_mchid",req.getParameter("official_mchid"));
        bizContentparams.put("auth_code",req.getParameter("auth_code"));
        bizContentparams.put("total_amount",req.getParameter("total_amount"));
        bizContentparams.put("out_trade_no",req.getParameter("out_trade_no"));
        bizContentparams.put("extend_params",req.getParameter("extend_params"));
        bizContentparams.put("goods_detail",req.getParameter("goods_detail"));
        bizContentparams.put("goods_tag",req.getParameter("goods_tag"));
        bizContentparams.put("attach", req.getParameter("attach"));
        bizContentparams.put("subject",req.getParameter("subject"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradePay POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    private void sign(Map<String, String> params) {
        String privateKey ="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMqnmT2/IbaX337ToGt+0y/hR816UEb9F3HUwCqSqesE5otgRmGS5ztQ+SSZSkDTEa/zd5+vPqvtSPwGoiiZ5t2yTPiLqW7JtxDsRJzUZZilkEHINETBgGLw/SOS6z/11RWkQFNfUMCiqoC8a7OVUqEiEMOtjRyPq6onml8j8BCnAgMBAAECgYEApx870p3ykYABgbfITZ3TT2L7McqytTg2w1rt9q3ojrYF4d7Na94X7uTB8Zqwj3EA3xdrCTcd+e6/B4f45n600ZrEggL85PgJ+padCifZMGRQVKyTWLXVujZEANj6h00DlBPA1aflX4lCuvr7jwTd+x/pPlkIOZEvccH3YdwrlKECQQDx3qy5EDDxUCTGzxnzt4E9/SfcwdXVaxME2/O9w4ZbJ04i7nH9JF5qqajMaohbSY7kQ1Dkbk9B1G1sUIgQGQEPAkEA1n5vaDx/QTr2fQ1M0XMs1fcAz5XqIyQBVyjCoy+WLQKsw880+55q9bAqstd3hgbCgRS5oGHGscVgGT/zYtxG6QJAf16T9qDgJTPfKhLFx+JK3SoDY+1vfgP08AokQTpF9pqsOxj2Tiho0CKoEX3IcMo7xVjtsF2mvzPuq6t8+30vYQJAczwADGccEuZjkHF5yLjw6I7ID75ByNlKIxJR5yvkqY/bU2NZ2qiUXCRl/CkIHKg7DPAefgypm84nP8l8r6saKQJAbcOdQzJ4DFfRAkPQ+pdPxDfxFLWrDVPdSG5yR1gmb1swNdQq88lvoSIS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rFs/ULICLNKZi88gdW5J528rgvs0s2g9ypuPSR1GO/FGjjfBnXqKRQKBgQC4E+VRDI+HzlH5e7gclSdiSHjSq+xzaD8m+SpNpZ2OJ4HsTVv9ZDOsWZ8KmC5DX2peKya9XEOG+SabIXEgG26TabfXJEQzaN2efR7LoS5QgaPPfEbQFshyrsyiKebggirLsuuWcNC47910MnhZEhEWus9fpbWb7Z+yvJAlCrD0QQKBgGs4I13sj5yOHeByXOkkrv0m8G3j7E98dbCWTL3d3G2RQNaoTchfEbBPgEFxV8mJS0PvmpJzA/3Wpjx9hzmi2TkfUSu0xXbIHVaXkG73haiEOgN+OTFcJax2ULnRzXWk8ABV2TUH/ZW4JH9zm3pD1g8F2SVcNEef2FR1rgTQH5nFAoGAU/HlXZ6y19vQUmtwjxR0nrPpQkapdSIynOlR3gSTwS1YuYPAmk0MG8j6SiaTPhTONeVekBZjS8dpR0cxoLQ3A6i947UCNjfLnt5pTRJ8j72n9kYrineqKY2gQ41+y1Tn6zQoJOin5NTPrQce/wLIbmVIifo8qlX/jzLRuJlNJgM=";
        final String sign_type = params.get("sign_type");
        final String sign = SignatureHelper.sign(params, "RSA".equalsIgnoreCase(sign_type) ? privateKey : privayeKey2, sign_type);
        params.put("sign",sign);
    }

    @RequestMapping(value="/sendTradeJsPay",method= RequestMethod.POST)
    public ModelAndView sendTradeJsPay(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("pay_type",req.getParameter("pay_type"));
        bizContentparams.put("mch_id",req.getParameter("mch_id"));
        bizContentparams.put("sub_mchid",req.getParameter("sub_mchid"));
        bizContentparams.put("official_mchid",req.getParameter("official_mchid"));
        bizContentparams.put("total_amount",req.getParameter("total_amount"));
        bizContentparams.put("out_trade_no",req.getParameter("out_trade_no"));
        bizContentparams.put("extend_params",req.getParameter("extend_params"));
        bizContentparams.put("goods_detail",req.getParameter("goods_detail"));
        bizContentparams.put("goods_tag",req.getParameter("goods_tag"));
        bizContentparams.put("attach", req.getParameter("attach"));
        bizContentparams.put("subject",req.getParameter("subject"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradeJsPay POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    @RequestMapping(value="/sendTradeQuery",method= RequestMethod.POST)
    public ModelAndView sendTradeQuery(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("mch_id",req.getParameter("mch_id"));
        bizContentparams.put("sub_mchid",req.getParameter("sub_mchid"));
        bizContentparams.put("out_trade_no",req.getParameter("out_trade_no"));
        bizContentparams.put("trade_no",req.getParameter("trade_no"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradeQuery POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    @RequestMapping(value="/sendTradeClose",method= RequestMethod.POST)
    public ModelAndView sendTradeClose(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("mch_id",req.getParameter("mch_id"));
        bizContentparams.put("sub_mchid",req.getParameter("sub_mchid"));
        bizContentparams.put("out_trade_no",req.getParameter("out_trade_no"));
        bizContentparams.put("trade_no",req.getParameter("trade_no"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradeClose POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    @RequestMapping(value="/sendTradeRevoke",method= RequestMethod.POST)
    public ModelAndView sendTradeRevoke(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("mch_id",req.getParameter("mch_id"));
        bizContentparams.put("sub_mchid",req.getParameter("sub_mchid"));
        bizContentparams.put("out_trade_no",req.getParameter("out_trade_no"));
        bizContentparams.put("trade_no",req.getParameter("trade_no"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradeRevoke POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    @RequestMapping(value="/sendTradeRefund",method= RequestMethod.POST)
    public ModelAndView sendTradeRefund(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("mch_id",req.getParameter("mch_id"));
        bizContentparams.put("sub_mchid",req.getParameter("sub_mchid"));
        bizContentparams.put("out_trade_no",req.getParameter("out_trade_no"));
        bizContentparams.put("trade_no",req.getParameter("trade_no"));
        bizContentparams.put("refund_amount",req.getParameter("refund_amount"));
        bizContentparams.put("refund_reason",req.getParameter("refund_reason"));
        bizContentparams.put("out_refund_no",req.getParameter("out_refund_no"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradeRefund POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    @RequestMapping(value="/sendTradeRefundQuery",method= RequestMethod.POST)
    public ModelAndView sendTradeRefundQuery(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("mch_id",req.getParameter("mch_id"));
        bizContentparams.put("sub_mchid",req.getParameter("sub_mchid"));
        bizContentparams.put("out_trade_no",req.getParameter("out_trade_no"));
        bizContentparams.put("trade_no",req.getParameter("trade_no"));
        bizContentparams.put("out_refund_no",req.getParameter("out_refund_no"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradeRefundQuery POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    @RequestMapping(value="/sendAuthOpenid",method= RequestMethod.POST)
    public ModelAndView sendAuthOpenid(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("redirect_url",req.getParameter("redirect_url"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendAuthOpenid POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }

    @RequestMapping(value="/sendTradeDataDownload",method= RequestMethod.POST)
    public ModelAndView sendTradeDataDownload(HttpServletRequest req, HttpServletResponse resp){
        Map<String,String> params = new HashMap<String, String>();
        params.put("api_name",req.getParameter("api_name"));
        params.put("service_name",req.getParameter("service_name"));
        params.put("timestamp",req.getParameter("timestamp"));
        params.put("charset",req.getParameter("charset"));
        params.put("version",req.getParameter("version"));
        params.put("sign",req.getParameter("sign"));
        params.put("sign_type",req.getParameter("sign_type"));
        params.put("app_auth_token",req.getParameter("app_auth_token"));

        Map<String,String> bizContentparams = new HashMap<String, String>();
        bizContentparams.put("bill_date",req.getParameter("bill_date"));

        JSONObject jsonObj = JSONObject.fromObject(bizContentparams);
        params.put("biz_content",jsonObj.toString());

        System.out.println("sendTradeDataDownload POST-Params:"+params);
        sign(params);
        HttpRequestResult result = HttpClientManager.getInstance().executePostMethodIngoreCert("http://d3609.opengateway2.yunzong:12335/gateway.do",params);
        String responseResult = result.getResponseTxt();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("responseResult::::::"+responseResult);
        modelAndView.addObject("responseResult", responseResult);
        modelAndView.setViewName("/result");
        return modelAndView;
    }
}
